<!-- 通讯录 -->
<template>
  <div class="app-container flex-row">
    <div class="conmmon-container-left scroll__y__content scrollbar-no-width" style="width:280px">
      <organization-tree v-model="listQuery.orgId" @selectedOrg="selectedOrg" />
    </div>
    <div class="conmmon-container-main flex-main">
      <template v-if="currentSelectedOrg.id">
        <!--搜索条件-->
        <div class="filter-container">
          <el-form :model="listQuery" :inline="true">
            <el-form-item>
              <el-input v-model="listQuery.keyword" placeholder="请输入真实姓名、手机号、用户名、职位" class="filter-item" style="width:300px" @keyup.enter.native="handleFilter" />
            </el-form-item>
            <el-form-item>
              <el-button plain type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
              <el-button plain icon="el-icon-refresh" @click="handleReset">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
        <!--列表-->
        <div class="table-container flex-main">
          <div class="table-container-header">
            <el-tabs v-model="listQuery.isInner" @tab-click="handleTabClick">
              <el-tab-pane label="内部员工" name="1" />
              <el-tab-pane label="外部员工" name="0" />
            </el-tabs>
            <table-toolbar :create="false" :remove="false" @refresh="handleFilter">
              <template slot="rightTool">
                <el-checkbox v-model="listQuery.includeChildren" style="line-height: 33px;">是否包含下级部门</el-checkbox>
              </template>
            </table-toolbar>
          </div>
          <el-table
            ref="multipleTable"
            :data="list"
            header-cell-class-name="table-header"
            fit
            height="100%"
            highlight-current-row
            :row-class-name="toggleRowClass"
            @sort-change="sortChange">
            <el-table-column align="center" prop="id" width="60" label="#">
              <template slot-scope="scope">{{ scope.$index+1 }}</template>
            </el-table-column>
            <el-table-column :show-overflow-tooltip="true" label="头像" prop="avatar" width="70" align="center">
              <template slot-scope="scope">
                <img :src="common.getFileView(scope.row.avatar)" class="common-round-image-32" data-default="avatar" @error="handleImageError">
              </template>
            </el-table-column>
            <el-table-column :show-overflow-tooltip="true" label="用户名" prop="userName" width="100">
              <template slot-scope="scope">
                <span class="color-primary common-cursor" @click="handleLookDetails(scope.row)">{{ scope.row.userName }}</span>
              </template>
            </el-table-column>
            <el-table-column :show-overflow-tooltip="true" label="员工号" width="100" prop="userCode" />
            <el-table-column :show-overflow-tooltip="true" label="姓名" prop="realName" width="100" />
            <el-table-column label="性别" prop="gender" width="80" align="center">
              <template slot-scope="scope">{{ scope.row.gender==1 ?'男': scope.row.gender==2 ? '女' : '保密' }}</template>
            </el-table-column>
            <el-table-column :show-overflow-tooltip="true" label="手机" prop="mobile" width="110" />
            <el-table-column :show-overflow-tooltip="true" label="座机" prop="phone" width="150" />
            <el-table-column label="加入部门" min-width="280" show-overflow-tooltip>
              <template slot-scope="scope">
                <div v-for="item in scope.row.organizationUsers" :key="item.organizationId" :class="item.organizationType===1?'color-primary':'color-default'">  {{ item._fullOrgName }} </div>
              </template>
            </el-table-column>
            <el-table-column prop="userOrgJob" width="150" label="部门任职" show-overflow-tooltip>
              <template slot-scope="scope">
                <div v-for="item in scope.row.organizationUsers" :key="item.organizationId" :class="item.organizationType===1?'color-primary':'color-default'">  {{ item.userOrgJob?item.userOrgJob:'-' }} </div>
              </template>
            </el-table-column>
            <el-table-column prop="userOrgPhone" width="150" label="办公电话" show-overflow-tooltip>
              <template slot-scope="scope">
                <div v-for="item in scope.row.organizationUsers" :key="item.organizationId" :class="item.organizationType===1?'color-primary':'color-default'">  {{ item.userOrgPhone?item.userOrgPhone:'-' }} </div>
              </template>
            </el-table-column>
            <el-table-column prop="addTime" width="150" sortable label="加入时间">
              <template slot-scope="scope">
                <div v-for="item in scope.row.organizationUsers" :key="item.organizationId" :class="item.organizationType===1?'color-primary':'color-default'">  {{ item.addTime | formatDate('yyyy-MM-dd hh:mm:ss') }} </div>
              </template>
            </el-table-column>
            <el-table-column prop="gmtCreated" width="150" sortable label="录入时间">
              <template slot-scope="scope">{{ scope.row.gmtCreated | formatDate }}</template>
            </el-table-column>

          </el-table>
        </div>
        <div class="pagination_handler">
          <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="fetchData" />
        </div>
      </template>
      <el-empty v-if="!currentSelectedOrg.id" :description="'该分类下暂未添加任何组织机构'">
        赶快联系管理员去新增吧！
      </el-empty>
    </div>

    <user-details ref="userDetails" />
  </div>
</template>

<script>

export default {
  components: { },
  data() {
    return {
      currentSelectedOrg: {},
      userId: '',
      list: null,
      listLoading: true,
      total: 0,
      listQuery: {
        page: 1,
        limit: this.common.PAGE_LIMIT_DEFAULT,
        sort: '+userPinyin',
        orgId: '',
        keyword: '',
        includeChildren: true,
        isInner: '1'
      }
    }
  },
  computed: {

  },
  watch: {
    'listQuery.orgId'(val) {
      if (val) {
        this.handleReset()
      }
    },
    'listQuery.includeChildren'(val) {
      this.listQuery = {
        ...this.listQuery,
        page: 1
      }
      this.fetchData()
    }
  },
  created() {

  },
  methods: {
    toggleRowClass({ row }) {
      if (row.isLocked === 1) {
        return 'common-default-row'
      }
      return ''
    },
    handleTabClick() {
      this.handleReset()
    },
    selectedOrg(val) {
      this.currentSelectedOrg = val
      this.listQuery = {
        ...this.listQuery,
        orgId: this.currentSelectedOrg.id,
        keyword: ''
      }
    },

    async fetchData() {
      this.listLoading = true
      const response = await this.$store.dispatch('userPageV2', this.listQuery)
      this.listLoading = false
      this.list = response.records
      this.total = response.total
    },
    // 固定写法
    sortChange(data) {
      const { prop, order } = data
      if (prop) {
        if (order === 'ascending') {
          this.listQuery.sort = '+' + prop
        } else {
          this.listQuery.sort = '-' + prop
        }
      } else {
        this.listQuery.sort = '+userPinyin'
      }
      this.handleFilter()
    },
    // 固定写法
    handleCommand(val) {
    },
    /**
     * 搜索
     */
    handleFilter() {
      this.listQuery = {
        ...this.listQuery,
        page: 1
      }
      this.fetchData()
    },
    /**
     * 重置
     */
    handleReset() {
      this.listQuery = {
        ...this.listQuery,
        page: 1,
        limit: this.common.PAGE_LIMIT_DEFAULT,
        sort: '+userPinyin',
        keyword: '',
        includeChildren: true
      }
      this.fetchData()
    },
    async handleLookDetails(row) {
      this.$refs.userDetails.init(row.id)
    }
  }
}
</script>
<style lang="scss" scoped>
</style>

